﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using YouWei.Infrastructure.Service;
using YouWei.Utility;

namespace ArticleCrawling
{
    public partial class Form1 : Form
    {
        //老司机来聊车
        private string lsj = "http://bbs.cheshi.com/forum-news-1450-1.html";
        //车友随手拍
        private string ssp = "http://bbs.cheshi.com/forum-news-1454-1.html";
        //游记
        private string yj = "http://bbs.cheshi.com/forum-news-1449-1.html";
        //摄影大咖
        private string sydk = "http://bbs.cheshi.com/forum-news-1452-1.html";

        /// <summary>
        /// 是否在抓取
        /// 抓取顺序 lsj ssp yj sydk 依次循环
        /// </summary>
        private int CurrentIndex = 0;

        private List<string> urlLinks = new List<string>();

        BbsScraping bbs = new BbsScraping();

        public Form1()
        {
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;
            timer1.Start();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
            if(CurrentIndex<1 && urlLinks.Count < 1)
            {
                urlLinks = bbs.GetCommentList(webBrowser1.DocumentText);
                if (urlLinks.Count > 0)
                {
                    webBrowser1.Url = new Uri(urlLinks[CurrentIndex]);
                }
            }
            else
            {
                bbs.GetCircleToComent(webBrowser1.DocumentText);
                CurrentIndex++;
                if (CurrentIndex == urlLinks.Count)
                {
                    CurrentIndex = 0;
                }
                Thread.Sleep(10 * 1000);
                webBrowser1.Url = new Uri(urlLinks[CurrentIndex]);
            }
        }

        /// 重写错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Error(object sender, HtmlElementErrorEventArgs e)
        {
            //忽略该错误并抑制错误对话框 
            e.Handled = true;
        }

        private void WirteLog(string msg)
        {
            string filePath = string.Format("{0}/err.txt", Application.StartupPath);
            using (StreamWriter sw = new StreamWriter(filePath, true))
            {
                sw.WriteLine(msg);
                sw.Dispose();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string crawling = ConfigHelper.GetAppSettingValue("CurCrawling");
            if (urlLinks.Count>0)
            {
                return;
            }
            else
            {
                switch (crawling)
                {
                    case "lsj":
                        webBrowser1.Url = new Uri(ssp);
                        ConfigHelper.UpdateAppConfig("CurCrawling", "ssp");
                        break;
                    case "ssp":
                        webBrowser1.Url = new Uri(yj);
                        ConfigHelper.UpdateAppConfig("CurCrawling", "yj");
                        break;
                    case "yj":
                        webBrowser1.Url = new Uri(sydk);
                        ConfigHelper.UpdateAppConfig("CurCrawling", "sydk");
                        break;
                    case "sydk":
                        webBrowser1.Url = new Uri(lsj);
                        ConfigHelper.UpdateAppConfig("CurCrawling", "lsj");
                        break;
                          
                }
            }
       
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1_Tick(null, null);
        }
    }
}
